unit uFrmSelectObjects;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls, Grids, gnugettext, CheckLst, DBCtrls, DB,
  OracleData, uutils, uSQLs, umainutils;

type
  Tfrm_SelectObjects = class(TForm)
    btn_Add: TButton;
    btn_Remove: TButton;
    Label1: TLabel;
    Label2: TLabel;
    btn_Ok: TButton;
    stg_Selected: TStringGrid;
    stg_Available: TStringGrid;
    OracleDataSet1: TOracleDataSet;
    DataSource1: TDataSource;
    procedure FormCreate(Sender: TObject);
    procedure btn_CancelClick(Sender: TObject);
    procedure btn_AddClick(Sender: TObject);
    procedure btn_RemoveClick(Sender: TObject);
    procedure btn_OkClick(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure stg_AvailableDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
    procedure stg_SelectedDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
  private
    m_TableNm, m_QueryAvailable, m_QuerySelected, m_Param1: string;
    m_MasterGrid: TStringGrid;
    { Private declarations }
  public
    { Public declarations }
    procedure ShowSelectObjectsForm(Grid: TStringGrid; ShowForm: boolean; TableNm, QueryAvailable, QuerySelected: string; Param1: string = '');
  end;

var
  frm_SelectObjects: Tfrm_SelectObjects;

implementation

uses uFrmMain;
{$R *.dfm}

procedure Tfrm_SelectObjects.btn_AddClick(Sender: TObject);
var
  i: Integer;
begin
  if stg_Available.Cells[0, stg_Available.Row] <> '' then
  begin
    if m_TableNm = 'LOGIN_FORM_PROFILES' then
    begin
      for i := stg_Available.Selection.Top to stg_Available.Selection.Bottom do
      begin
        InsertDataIntoDB('LOGIN_FORM_PROFILES', 'LOGIN_FORM_ID, PROFILE_ID', stg_Available.Cells[0, i] + ',' + m_Param1);
      end;
    end
    else if m_TableNm = 'TOURNAMENTS' then
    begin
      for i := stg_Available.Selection.Top to stg_Available.Selection.Bottom do
      begin
        InsertDataIntoDB('TOURNAMENT_TABLES', 'TABLE_ID, TOURNAMENT_ID', stg_Available.Cells[0, i] + ',' + m_Param1);
      end;
    end
    else if m_TableNm = 'USERS' then
    begin
      for i := stg_Available.Selection.Top to stg_Available.Selection.Bottom do
      begin
        InsertDataIntoDB('USER_HAS_PROFILE', 'PROFILE_ID, USER_ID', stg_Available.Cells[0, i] + ',' + m_Param1);
      end;
    end
    else if m_TableNm = 'PROFILES' then
    begin
      for i := stg_Available.Selection.Top to stg_Available.Selection.Bottom do
      begin
        InsertDataIntoDB('PROFILE_HAS_PRIVILEGE', 'PRIVILEGE_ID, PROFILE_ID', stg_Available.Cells[0, i] + ',' + m_Param1);
      end;
    end
    else if m_TableNm = 'CASH_GAMES' then
    begin
      for i := stg_Available.Selection.Top to stg_Available.Selection.Bottom do
      begin
        if stg_Selected.Cells[1, 0] <> '' then
        begin
          ShowMessage(_('Only one table for cash game. First remove table!'));
          exit;
        end;
        UpdateDataToDB('CASH_GAMES', 'TABLE_ID=' + stg_Available.Cells[0, i], 'ID=' + FrmMain.stg_CashGames.Cells[0, FrmMain.stg_CashGames.Row]);
      end;
    end;
    ShowSelectObjectsForm(nil, false, m_TableNm, m_QueryAvailable, m_QuerySelected);
  end;
end;

procedure Tfrm_SelectObjects.btn_RemoveClick(Sender: TObject);
var
  i: Integer;
begin
  if stg_Selected.Cells[0, stg_Selected.Row] <> '' then
  begin
    if m_TableNm = 'TOURNAMENTS' then
    begin
      for i := stg_Selected.Selection.Top to stg_Selected.Selection.Bottom do
      begin
        // CHECK IF TABLE IS OCCUPIED
        FrmMain.OracleQuery1.Close;
        FrmMain.OracleQuery1.SQL.Clear;
        FrmMain.OracleQuery1.SQL.Text := (
          'SELECT PLAYING FROM TOURNAMENT_TABLES TT JOIN GET_PLAYERS_PLAY_TICK_BY_TABLE GPP ON GPP.TABLE_ID=TT.TABLE_ID AND TT.TOURNAMENT_ID=GPP.TOURNAMENT_ID WHERE TT.ID=' + stg_Selected.Cells[2, i]
          );
        FrmMain.OracleQuery1.Execute;
        if not FrmMain.OracleQuery1.eof then
        begin
          if FrmMain.OracleQuery1.Field(0) <> 0 then
          begin
            ShowMessage(_('Players are on the table!'));
            exit;
          end
          else
          begin
            DeleteDataFromDB('TOURNAMENT_TABLES', 'ID=' + stg_Selected.Cells[2, i]);
          end;
        end
        else
        begin
          DeleteDataFromDB('TOURNAMENT_TABLES', 'ID=' + stg_Selected.Cells[2, i]);
        end;
      end;
    end
    else if m_TableNm = 'LOGIN_FORM_PROFILES' then
    begin
      for i := stg_Selected.Selection.Top to stg_Selected.Selection.Bottom do
      begin
        DeleteDataFromDB('LOGIN_FORM_PROFILES', 'ID=' + stg_Selected.Cells[2, i]);
      end;
    end
    else if m_TableNm = 'USERS' then
    begin
      for i := stg_Selected.Selection.Top to stg_Selected.Selection.Bottom do
      begin
        DeleteDataFromDB('USER_HAS_PROFILE', 'ID=' + stg_Selected.Cells[2, i]);
      end;
    end
    else if m_TableNm = 'PROFILES' then
    begin
      for i := stg_Selected.Selection.Top to stg_Selected.Selection.Bottom do
      begin
        DeleteDataFromDB('PROFILE_HAS_PRIVILEGE', 'ID=' + stg_Selected.Cells[2, i]);
      end;
    end
    else if m_TableNm = 'CASH_GAMES' then
    begin
      for i := stg_Selected.Selection.Top to stg_Selected.Selection.Bottom do
      begin
        UpdateDataToDB('CASH_GAMES', 'TABLE_ID=NULL', 'ID=' + FrmMain.stg_CashGames.Cells[0, FrmMain.stg_CashGames.Row]);
      end;
    end;
    ShowSelectObjectsForm(nil, false, m_TableNm, m_QueryAvailable, m_QuerySelected);
  end;
end;

procedure Tfrm_SelectObjects.btn_CancelClick(Sender: TObject);
begin
  Close();
end;

procedure Tfrm_SelectObjects.btn_OkClick(Sender: TObject);
begin
  if m_TableNm = 'USERS' then
  begin
    FrmMain.stg_UsersSelectCell(self,0,FrmMain.stg_Users.Row, canselect);
  end
  else if m_TableNm = 'LOGIN_FORM_PROFILES' then
  begin
    FrmMain.GetGridData(FrmMain.stg_LoginForms, SQL_LoginFormsProfiles + FrmMain.stg_Profiles.Cells[0, FrmMain.stg_Profiles.Row], true);
  end
  else if m_TableNm = 'TOURNAMENTS' then
  begin
    FrmMain.GetGridData(FrmMain.stg_TournamentTables, SQL_TournamentTables + FrmMain.stg_Tournaments.Cells[0, FrmMain.stg_Tournaments.Row] + ' Order by name', false);
  end
  else if m_TableNm = 'CASH_GAMES' then
  begin
    FrmMain.GetGridData(FrmMain.stg_CashGames, SQL_CashGames, false);
    // FrmMain.SelectGridRow(FrmMain.stg_CashGames, FrmMain.stg_CashGames.Row);
  end;
  Close;
end;

procedure Tfrm_SelectObjects.FormCreate(Sender: TObject);
begin
  gnugettext.TranslateComponent(self);
  stg_Available.ColWidths[0] := -1;
  stg_Selected.ColWidths[0] := -1;
  stg_Available.ColWidths[1] := stg_Available.Width - 5;
  stg_Selected.ColWidths[1] := stg_Selected.Width - 5;
  stg_Selected.ColWidths[2] := -1; // ID ROW TABLE
end;

procedure Tfrm_SelectObjects.FormKeyPress(Sender: TObject; var Key: Char);
begin
  // The Escape key is #27.
  if Key = #27 then
    Close;
end;

procedure Tfrm_SelectObjects.ShowSelectObjectsForm(Grid: TStringGrid; ShowForm: boolean; TableNm, QueryAvailable, QuerySelected, Param1: string);
var
  i, j: Integer;
begin
  m_QueryAvailable := QueryAvailable;
  m_QuerySelected := QuerySelected;
  m_TableNm := TableNm;
  if Grid <> nil then
    m_MasterGrid := Grid;
  // clear grids
  for j := 0 to stg_Available.ColCount - 1 do
  begin
    for i := 0 to stg_Available.RowCount - 1 do
    begin
      stg_Available.Cells[j, i] := '';
    end;
  end;
  for j := 0 to stg_Selected.ColCount - 1 do
  begin
    for i := 0 to stg_Selected.RowCount - 1 do
    begin
      stg_Selected.Cells[j, i] := '';
    end;
  end;
  stg_Available.RowCount := 0;
  stg_Selected.RowCount := 0;
  OracleDataSet1.Close;
  OracleDataSet1.Session := FrmMain.OracleSession1;
  OracleDataSet1.SQL.Clear;
  OracleDataSet1.SQL.Text := (QueryAvailable);
  // ---.
  OracleDataSet1.ExecSQL;
  OracleDataSet1.open;
  DataSource1.DataSet := OracleDataSet1;
  FrmMain.OracleQuery1.SQL.Clear;
  FrmMain.OracleQuery1.SQL.Text := (QueryAvailable);
  FrmMain.OracleQuery1.Execute;
  i := 0;
  while not FrmMain.OracleQuery1.eof do
  begin
    stg_Available.Cells[0, i] := FrmMain.OracleQuery1.Field(0);
    stg_Available.Cells[1, i] := FrmMain.OracleQuery1.Field(1);
    inc(i);
    FrmMain.OracleQuery1.Next;
  end;
  stg_Available.RowCount := i;
  // -----------
  FrmMain.OracleQuery1.SQL.Clear;
  FrmMain.OracleQuery1.SQL.Text := (QuerySelected);
  FrmMain.OracleQuery1.Execute;
  i := 0;
  while not FrmMain.OracleQuery1.eof do
  begin
    stg_Selected.Cells[0, i] := FrmMain.OracleQuery1.Field(0);
    stg_Selected.Cells[1, i] := FrmMain.OracleQuery1.Field(1);
    stg_Selected.Cells[2, i] := FrmMain.OracleQuery1.Field(2);
    inc(i);
    FrmMain.OracleQuery1.Next;
  end;
  stg_Selected.RowCount := i;
  if ShowForm then
  begin
    m_Param1 := Param1;
    ShowModal();
  end;
end;

procedure Tfrm_SelectObjects.stg_AvailableDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
  FrmMain.GridDrawCell(Sender, ACol, ARow, Rect, State);
end;

procedure Tfrm_SelectObjects.stg_SelectedDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
  FrmMain.GridDrawCell(Sender, ACol, ARow, Rect, State);
end;

end.
